//
//  TerminalReceivingTableView.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/23.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "TerminalReceivingTableView.h"

#import "OrderHeaderView.h"//页眉
#import "TerminalReceivingFooterView.h"//页脚
#import "OrderFooterTotalString.h"//页脚合计String
#import "OrderDetailTableViewCell.h"//Cell



@implementation TerminalReceivingTableView
{
    UITableView *_tableView;
    
    EnterPasswordView *_enterPasswordView;
}

static NSString *const OrderHeaderViewID = @"OrderHeaderView";
static NSString *const TerminalReceivingFooterViewID = @"TerminalReceivingFooterView";
static NSString *const OrderDetailTableViewCellID = @"OrderDetailTableViewCell";

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 94)style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self addSubview:_tableView];
        
        [self registerTableView];
        
        [self registerForKeyboardNotifications];
    }
    return self;
}
#pragma mark === 注册TableView ===
- (void)registerTableView{
    //注册页眉
    [_tableView registerNib:[UINib nibWithNibName:@"OrderHeaderView" bundle:nil] forCellReuseIdentifier:OrderHeaderViewID];
    //注册页脚
    [_tableView registerNib:[UINib nibWithNibName:@"TerminalReceivingFooterView" bundle:nil] forCellReuseIdentifier:TerminalReceivingFooterViewID];
    //注册自定义cell
    [_tableView registerNib:[UINib nibWithNibName:@"OrderDetailTableViewCell" bundle:nil] forCellReuseIdentifier:OrderDetailTableViewCellID];
}

#pragma mark === TableViewDelegate ===
//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderDetailTableViewCellID];
    cell.goodsImage.backgroundColor = [UIColor yellowColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//页眉视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    OrderHeaderView *headView = [[[NSBundle mainBundle] loadNibNamed:@"OrderHeaderView" owner:nil options:nil] lastObject];
    headView.merchantImage.backgroundColor = [UIColor yellowColor];
    return headView;
}
//页脚视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    TerminalReceivingFooterView *footView = [[[NSBundle mainBundle] loadNibNamed:@"TerminalReceivingFooterView" owner:nil options:nil] lastObject];
    //页脚合计赋值
    footView.total.attributedText = [OrderFooterTotalString AttributedStringWithNum:@"2" andTotal:@"200.00" andFreight:@"5.00"];
    //确认收货
    [footView.affirmDelivery addTarget:self action:@selector(affirmDeliveryy) forControlEvents:UIControlEventTouchUpInside];
    return footView;
}
//cell高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return WIDTH / 7 * 2;
}
//页眉高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
//页脚高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100;
}

#pragma mark === 确认收货 ===
- (void)affirmDeliveryy{
    _enterPasswordView = [[EnterPasswordView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:_enterPasswordView];
    
    __weak TerminalReceivingTableView *weakView = self;
    _enterPasswordView.windowClick = ^(NSString *string){
        if ([string isEqualToString:@"hidden"] || [string isEqualToString:@"close"]) {
            [weakView.enterPasswordView removeFromSuperview];
        }else if ([string isEqualToString:@"forget"]){
            NSLog(@"忘记密码");
        }else{
            NSLog(@"输入密码:%@", string);
            [weakView.enterPasswordView removeFromSuperview];
        }
    };
}
#pragma mark === 键盘监听 ===
//监听键盘
- (void)registerForKeyboardNotifications{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
//键盘出现时
- (void)keyboardWillShow:(NSNotification *)notif{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    _enterPasswordView.frame = CGRectMake(0, -keyboardSize.height, WIDTH, HEIGHT);
}


@end
